import os
import sys
import time
import json

class Squashfs:
    def MountReadOnly(self, file, dir):
        os.system(f"pkexec mount '{file}' '{dir}'")

    def MountReadWriteAndNotSave(self, file, dir):
        dirPath = f"/dclc-mount/mount/program-mount-{time.time()}"
        if not os.path.exists(dirPath) or not os.path.exists("/to") or not os.path.exists("/temp"):
            os.system(f"pkexec mkdir -p '{dirPath}' /to /temp")
        self.MountReadOnly(file, dirPath)
        os.system(f"pkexec mount -t overlay -o 'lowerdir={dirPath},upperdir=/to,workdir=/temp' overlay '{dir}'")

    def Umount(self, path):
        os.system(f"pkexec umount '{path}'")

    def BuildLinkToRootPath(self, dirPath, head=""):
        for i in os.listdir(dirPath):
            rootPath = f"{head}/{i}"
            mountPath = f"{dirPath}/{i}"
            if os.path.exists(rootPath) and os.path.isdir(mountPath):
                self.BuildLinkToRootPath(mountPath, rootPath)
                continue
            if os.path.isfile(mountPath):
                os.system(f"pkexec ln -s '{mountPath}' '{rootPath}'")
            
    def RemoveLink(self, lists, listFile=""):
        if os.path.exists(listFile):
            with open(listFile, "r") as file:
                lists = json.loads(file)
        for i in lists:
            if os.path.islink(i):
                os.system(f"pkexec rm '{i}'")